<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<style>
	.error-text{
		line-height: 20px;
		font-size:14px;
		color: rgb(209, 91, 71);
	}
</style>
<script type="text/dialog">
	var setting = {
		check:{enable:true,nocheckInherit:true},
		view:{selectedMulti:false},
		data:{simpleData:{enable:true}}
	}
	var zNodes =[
		<c:forEach items="${roleTree}" var="menu">
		{
			id:'${menu.id}', 
			pId:'${menu.pid}', 
			name:'${menu.name}'
		},
		</c:forEach>
	];
	var tree = $.fn.zTree.init($("#menuTree"), setting, zNodes);
	//判断哪些资源需要选中
	<c:forEach items="${Prole.permissionList}" var="mid">
		var node = tree.getNodeByParam("id", ${mid});
		try{
			tree.checkNode(node, true, false);
		}catch(e){}
	</c:forEach>
	tree.expandAll(true);
	
	$('#roleEditForm').validate({
		errorElement: 'div',
     	errorClass: 'help-block',
     	focusInvalid: false,
		rules: {
          rolename:{
               required: true,
               remote:{         
                    url:"${pageContext.request.contextPath}/sys/role/checkRoleName.do",
                    data:{
                         rolename : function(){return encodeURIComponent($("#rolename").val());},
						 oldrolename : encodeURIComponent('${Prole.rolename}')
                    }
               }
          },
          remark:{
               required:true
          },
          seq:{
               number:true
          }
     },
	messages:{
		rolename: {remote: "角色名已存在."}
	},
	highlight: function (e) {
		$(e).closest('.form-group').removeClass('has-info').addClass('has-error');
	},
	success: function (e) {
		$(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
		$(e).remove();
	},
	submitHandler: function(form) {
		var ids = [], nodes = tree.getCheckedNodes(true);
		for(var i=0; i<nodes.length; i++) {
			ids.push(nodes[i].id);
		}
		$("#menuIds").val(ids);
		  $(form).ajaxSubmit({
				beforeSubmit: showRequest,
				success:       showResponse
		});  
	},  
	errorPlacement: function(error, element) {
		if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
					error.appendTo(element.parent().parent());
				} else {
					error.insertAfter(element);
				}
		}
	});

	//表单提交控制
	var options = { 
        beforeSubmit:  showRequest,  
        success:       showResponse
    };

function showRequest(formData, jqForm, options){
 	var queryString = $.param(formData);
 	art.dialog({id:'uploadDialog',title:'正在处理...',content:'<img src="../resources/admin/assets/images/loading2.gif">&nbsp;&nbsp;处理中，请稍后...',width:'250px',height:'120px',lock:true,background:'#ccc'});
 	return true;
 }
 function showResponse(data){
 	art.dialog.list['uploadDialog'].close();
 	<%--var data = eval('(' + data + ')');--%>
 	if(data.success){
 		$(".error-text").html("");
		art.dialog({id:'editRoleDialog'}).close();
		art.dialog({content:data.msg,resize:true,time:2,icon:'succeed',ok:function(){this.close()}});
		$('#RoleTable').DataTable().draw() ;		//重新刷新数据表格
 	}else{
 		$(".error-text").html(data.msg);
		return;
 	}
 }
</script>
<div class="scheduleBox">
	<h3 class="lighter block green">编辑角色 - ${Prole.rolename}</h3>
	<form class="form-horizontal" role= "form" id="roleEditForm" name="roleEditForm" method="post" action="${pageContext.request.contextPath}/sys/role/roleEdit.do">
		<input type="hidden" name="id" value="${Prole.id}">
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 角色名: </label>
			<div class="col-sm-8">
				<input type="text" id="rolename" class="col-sm-6" name="rolename" value="${Prole.rolename}"/>
			</div>
		</div>
		
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 备注: </label>
			<div class="col-sm-8 ">
				<input type="text"  class="col-sm-12"  name="remark" value="${Prole.remark}"/>
			</div>
		</div>
		
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 显示顺序: </label>
			<div class="col-sm-8 ">
				<input type="text"  class="col-sm-6" id="realname" name="seq" value="${Prole.seq}"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 角色授权: </label>
			<div class="col-sm-8 ">
				<div class="ztree" id="menuTree"></div>
				<input name="menuIds" id="menuIds" type="hidden"/>
			</div>
		</div>
		
		<div class="form-group">
			<div class="col-sm-4">
				<button type="submit" class="btn btn-sm btn-success">
				保存
				<i class="ace-icon fa fa-pencil bigger-110"></i>
				</button>
			</div>
		</div>
		
	</form>
</div>